import Wampy from 'wampy';

class Wamp {
  constructor() {
    this.client = new Wampy('ws://localhost:8080');
    this.client.options({
      reconnectInterval: 1000,
      maxRetries: 999,
      onConnect: function (welcomeDetails) {
        console.log('Yahoo! We are online! Details:', welcomeDetails);
      },
      onClose: function () {
        console.log('See you next time!');
      },
      onError: function () {
        console.log('Breakdown happened');
      },
      onReconnect: function () {
        console.log('Reconnecting...');
      },
      onReconnectSuccess: function (welcomeDetails) {
        console.log('Reconnection succeeded. Details:', welcomeDetails);
      }
    });
  }

  open() {
    console.log('open');
    console.log(this.client.options());
    this.client.connect('ws://localhost:8080');
    this.client.subscribe('chat.message.received', function (msg) {
      console.log('Received new chat message!');
      console.log(msg);
    });
  }

  close() {
    this.client.disconnect();
  }

  send() {
    this.client.publish('client.message', 'Hi guys!');
    this.client.publish('system.monitor.update');
  }
}

export default new Wamp();
